﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    /// <summary>
    /// Session数据模型绑定器
    /// </summary>
    public class SessionModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            IValueProvider p = new SessionValueProviderFactory().GetValueProvider(controllerContext);

            if (!string.IsNullOrEmpty(bindingContext.ModelName) && p.ContainsPrefix(bindingContext.ModelName))
            {
                
                object value = p.GetValue(bindingContext.ModelName).RawValue;
                if (value.GetType().Equals(bindingContext.ModelType))
                {
                    return value;
                }
            }
            return null;
        }
    }
}
